package com.lsq.baseadmin.modules.manage.controller;


import com.lsq.baseadmin.modules.manage.entity.Role;
import com.lsq.baseadmin.modules.manage.service.RoleMenuService;
import com.lsq.baseadmin.modules.manage.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lsq
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    
    private RoleService roleService;
    private RoleMenuService roleMenuService;
    public RoleController(RoleService roleService, RoleMenuService roleMenuService) {
        this.roleService = roleService;
        this.roleMenuService = roleMenuService;
    }

    @ApiOperation("角色列表")
    @GetMapping("/list")
    public ResponseEntity list(Role role) {
        List<Role> list = roleService.list(role);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("角色详情")
    @GetMapping("/detail")
    public ResponseEntity detail(@RequestParam("id") int id) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("role_id", id);
        return ResponseEntity.ok(roleMenuService.listByMap(map));
    }

    @ApiOperation("角色添加")
    @PostMapping("/add")
    public ResponseEntity add(@RequestBody Role role) {
        roleService.save(role);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation("角色修改")
    @PostMapping("/update")
    public ResponseEntity update(@RequestBody Role role) {
        roleService.updateById(role);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation("角色删除")
    @PostMapping("/delete")
    public ResponseEntity delete(int id) {
        roleService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation("角色菜单")
    @PostMapping("/updateMenu")
    public ResponseEntity updateMenu(@RequestParam("id") int id, @RequestParam("menuIds[]") Integer[] menuIds) {
        roleService.updateMenu(id, menuIds);
        return new ResponseEntity(HttpStatus.OK);
    }


}
